<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/plat-style.css">
    <link href="/css/plugins/jsTree/style.min.css" rel="stylesheet">
    <style>
        thead tr th{text-align: center!important;font-weight: bold!important;}
        tr td{text-align: center!important;}
        .layui-icon{
            font-size: 20px;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-warm layui-layout-right" style="margin-right: 20px;margin-top: 5px" onclick="showLayer('新增一级分类','/classify/classifyEdit?editType=0&level=0')">新增主规格</button>
                </div>
                <div class="layui-card-body">
                    <div id="classify">
                        <ul>
                            <c:forEach items="${list}" var="f" varStatus="i">
                                <%-- 一级分类 --%>
                                <li onclick="show(${f.id},1)" style="float:left;">${f.name} (${f.aliasName})
                                    <div class="first" data-id="${f.id }" style="float: right;margin-left: 30px;" hidden>
                                            <%-- 新增type=1；编辑type=2 --%>
                                        <a class="libutton" title="新增下级" onclick="showLayer('新增下级分类',
                                                '/classify/classifyEdit?id=${f.id}&editType=1&fName=${f.name}&level=1&firstId=${f.id}')">
                                            <i class="layui-icon layui-icon-add-1"></i></a>
                                        <a class="libutton" title="编辑" onclick="showLayer('编辑一级分类',
                                                '/classify/classifyEdit?id=${f.id}&editType=2&level=1&name=${f.name}&sort=${f.sort}&url=${f.url}&aliasName=${f.aliasName}')">
                                            <i class="layui-icon layui-icon-edit"></i></a>
                                        <a class="libutton" title="删除" onclick="checkSubordinate(${f.id},1)">
                                            <i class="layui-icon layui-icon-delete"></i></a>
                                    </div>
                                    <ul>
                                        <c:forEach items="${f.secondClassifyMains}" var="s">
                                            <%-- 二级分类 --%>
                                            <li onclick="show(${s.id},2)" data-jstree='{"type":"fl"}'> ${s.name } (${s.aliasName})
                                                <div class="second" data-id="${s.id }" style="float: right;margin-left: 30px;" hidden>
                                                    <a class="libutton" title="新增下级" onclick="showLayer('新增下级分类',
                                                            '/classify/classifyEdit?id=${f.id}&editType=1&fName=${f.name}&sName=${s.name}&level=2&firstId=${f.id}&secondId=${s.id}')">
                                                        <i class="layui-icon layui-icon-add-1"></i></a>
                                                    <a class="libutton" title="编辑" onclick="showLayer('编辑二级分类',
                                                            '/classify/classifyEdit?id=${s.id}&editType=2&level=2&fName=${f.name}&name=${s.name}&firstId=${f.id}&aliasName=${s.aliasName}&sort=${s.sort}')">
                                                        <i class="layui-icon layui-icon-edit"></i></a>
                                                    <a class="libutton" title="删除" onclick="checkSubordinate(${s.id},2)">
                                                        <i class="layui-icon layui-icon-delete"></i></a>
                                                </div>
                                                <ul>
                                                    <c:forEach items="${s.thirdClassifyMains}" var="v">
                                                        <%-- 三级分类 --%>
                                                        <li onclick="show(${v.id},3)" data-jstree='{"type":"fl"}'>${v.name } (${v.aliasName})
                                                            <div class="third" data-id="${v.id }"
                                                                 style="float: right;margin-left: 30px;" hidden>
                                                                <a class="libutton" title="编辑" onclick="showLayer('编辑三级分类',
                                                                        '/classify/classifyEdit?id=${v.id}&editType=2&level=3&fName=${f.name}&sName=${s.name}&firstId=${f.id}&secondId=${s.id}&name=${v.name}&aliasName=${v.aliasName}&sort=${v.sort}')">
                                                                    <i class="layui-icon layui-icon-edit"></i></a>
                                                                <a class="libutton" title="删除" onclick="checkSubordinate(${v.id},3)">
                                                                    <i class="layui-icon layui-icon-delete"></i></a>
                                                            </div>
                                                        </li>
                                                    </c:forEach>
                                                </ul>
                                            </li>
                                        </c:forEach>
                                    </ul>
                                </li>
                            </c:forEach>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script src="/js/plugins/jsTree/jstree.min.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$,
            element = layui.element,
            layer = layui.layer;
        element.render();
    });

    function showLayer(title,url){
        var index = layer.open({
            type: 2,
            title: title,
            content: url,
            area: ['700px', '580px']
        });
    }

    $(function () {
        //jstree插件初始化
        $("#classify").jstree({
            "core": {
                "check_callback": true
            },
            "plugins": ["types", "dnd"],
            "types": {
                "default": {
                    "icon": false
                },
                "fl": {
                    "icon": false
                }
            }
        });
    })

    //显示小图标
    function show(id, flag) {
        if (flag == 1) {
            $(".first").each(function (e) {
                var data = $(this).attr("data-id");
                if (data == id) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        } else if (flag == 2) {
            $(".second").each(function (e) {
                var data = $(this).attr("data-id");
                if (data == id) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        } else if (flag == 3) {
            $(".third").each(function (e) {
                var data = $(this).attr("data-id");
                if (data == id) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        }
    }

    function deleteClassCategory(id,level,msg){
        layer.confirm(msg,function(index){
            $.ajax({
                url: "/classify/deleteClassCategory",
                data: {"id":id,'level':level},
                dataType: "json",
                type: "post",
                success: function (data) {
                    if(data.status){
                        layer.close(index);
                        layer.msg(data.desc);
                        window.setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    }else{
                        layer.close(index);
                        layer.msg(data.desc);
                    }
                }
            });
        });
    }

    //删除前检查下级是否存在有分类
    function checkSubordinate(id,level){
        if(level < 3){
            layer.confirm('确认要删除吗？',function(index){
                $.ajax({
                    url: "/classify/queryClassifyByPid",
                    data: {"id":id,'level':level},
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if(data){
                            // 下级有数据
                            layer.close(index);
                            deleteClassCategory(id,level,'该分类下级还有数据，要删除吗？');
                        }else{
                            // 下级没数据
                            layer.close(index);
                            deleteClassCategory(id,level,'该分类下级已无数据，要删除吗？');
                        }
                    }
                });
            });
        }else if(level == 3){
            deleteClassCategory(id,level,'要删除这个分类吗？');
        }
    }
</script>
</body>
</html>